

#Load Packages
library(ggplot2)
library(gridExtra)
library(haven)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

#  Load Data
ClarkRogersData <- read_dta("Data/Formatted Data/Clark-Rogers-RegressionData.dta")

state.scatter <- data.frame(matrix(NA, nrow(ClarkRogersData),0))
state.scatter$state_guess <- ClarkRogersData$Belief_WomenStateLeg
state.scatter$state <- ClarkRogersData$True_WomenStateLeg

#############################################################
#### Figure 1a: Scatter Plot ####
#############################################################
head(state.scatter)

scatter<-ggplot(state.scatter, aes(x=state_guess, y=state)) +
  geom_point(size=4, shape=1)+
  geom_smooth(method = "lm", size = 1.5, color="grey45")+
  ylab("% Women in R's State Leg. \n")+
  xlab("\n Respondent Estimates")+
  ggtitle(" ")+
  theme_bw()
scatter<-scatter+theme_bw() + theme(panel.grid.major = element_blank(),
                                    panel.grid.minor = element_blank(), panel.border=element_blank())
scatter<-scatter+  theme(axis.text=element_text(size=17),
                         axis.title=element_text(size=17, face="bold"),
                         plot.title=element_text(size=19, face="bold"))+
  scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
scatter<-scatter+theme(legend.justification=c(.5,.85), legend.position=c(.05,.95), legend.text=element_text(size=14),
                       legend.title=element_text(size=16))
scatter<-scatter+theme(legend.key.width=unit(2,"cm"))
scatter_Rogers_gender <-scatter+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))

scatter

#############################################################
#### Figure 1b: Density Plot ####
#############################################################

cong.density <- data.frame(matrix(NA, nrow(ClarkRogersData),0))
cong.density$r_estimate <- ClarkRogersData$Belief_WomenCongress

cong.density$r_estimate<-ifelse(cong.density$r_estimate>50, 50, cong.density$r_estimate)
table(cong.density$r_estimate)

density<-ggplot(cong.density, aes(r_estimate))+
  geom_density(adjust=1, size=1.5, alpha=.45)
density<-density+scale_fill_manual(values=c("white"))
#density<-density+scale_color_manual(values=c("grey39", "grey55"), name="Year", labels=c("2016", "2018"))
density<-density+labs(x="\n Estimate % Women in Congress", y=" \n \n Density \n",
                      title= )
density<-density+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
density<-density + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour = "black"))
density_Rogers_gender <-density+theme(axis.text=element_text(size=17),
                                      axis.title=element_text(size=17, face="bold"),
                                      plot.title=element_text(size=19, face="bold"))


tiff(filename = "Figures/Appendix/Figure-A-2.tiff",
    width = 12, height = 6, units = "in", pointsize = 12, res=1200)

test <- grid.arrange (density_Rogers_gender, scatter_Rogers_gender,  ncol=2)

dev.off()